package com.west.common.base.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.west.common.base.adapter.BaseGeneralRecyclerAdapter;
import com.west.common.base.adapter.BaseRecyclerAdapter;
import com.west.common.base.mvp.BaseListPresenter;
import com.west.common.base.mvp.BaseListView;
import com.west.common.common.R;
import com.west.common.uitils.TDevice;
import com.west.common.uitils.TLog;
import com.west.common.widget.EmptyLayout;
import com.west.common.widget.RecyclerRefreshLayout;

import java.util.Date;
import java.util.List;


/**
 * MVP刷新列表基类，再无逻辑判断，清晰自然
 */

public abstract class BaseRecyclerFragment<Presenter extends BaseListPresenter, Model> extends BaseFragment
        implements BaseListView<Presenter, Model>,
        BaseRecyclerAdapter.OnItemClickListener,
        RecyclerRefreshLayout.SuperRefreshLayoutListener,
        BaseGeneralRecyclerAdapter.Callback {

    protected RecyclerRefreshLayout mRefreshLayout;
    protected RecyclerView mRecyclerView;
    protected EmptyLayout mErrorLayout;
    protected BaseRecyclerAdapter<Model> mAdapter;
    protected Presenter mPresenter;
    protected ImageView mIvBackTop;
    protected long mDistance;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_recycler;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mRefreshLayout = (RecyclerRefreshLayout) mRoot.findViewById(R.id.refreshLayout);
        mRefreshLayout.setSuperRefreshLayoutListener(this);
        mErrorLayout = (EmptyLayout) mRoot.findViewById(R.id.error_layout);
        mRecyclerView = (RecyclerView) mRoot.findViewById(R.id.recyclerView);
        mIvBackTop = (ImageView) mRoot.findViewById(R.id.iv_back_top);
        mAdapter = getAdapter();
        mRecyclerView.setLayoutManager(getLayoutManager());
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        mRefreshLayout.setColorSchemeResources(
                R.color.swiperefresh_color1, R.color.swiperefresh_color2,
                R.color.swiperefresh_color3, R.color.swiperefresh_color4);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //mDistance += dy;
                if(mDistance >= 2 * TDevice.getScreenHeight(mContext)){
                    mIvBackTop.setVisibility(View.VISIBLE);
                }else{
                    mIvBackTop.setVisibility(View.GONE);
                }
                TLog.e("comment", " mDistance " + mDistance + " dy " + dy + " height " + 2 * TDevice.getScreenHeight(mContext));
            }
        });

        mIvBackTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecyclerView.scrollToPosition(0);
                mDistance = 0;
            }
        });

        mErrorLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mErrorLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                onRefreshing();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mRefreshLayout.setRefreshing(true);
                if(mPresenter == null)
                    return;
                mPresenter.onRefreshing();
            }
        });
    }

    @Override
    public void onItemClick(int position, long itemId) {
        Model model = mAdapter.getItem(position);
        if (model != null)
            onItemClick(model, position);
    }

    @Override
    public void onRefreshing() {
        if(mPresenter == null)
            return;
        mAdapter.setState(BaseRecyclerAdapter.STATE_HIDE, true);
        mPresenter.onRefreshing();
    }

    @Override
    public void onLoadMore() {
        mPresenter.onLoadMore();
        mAdapter.setState(BaseRecyclerAdapter.STATE_LOADING, true);
    }

    @Override
    public void onRefreshSuccess(List<Model> data) {

        if(mAdapter == null){
            getAdapter();
        }
        mDistance = 0;
        mIvBackTop.setVisibility(View.GONE);
        mAdapter.resetItem(data);
        if (mAdapter.getCount() > 0) {
            mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            mRefreshLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.VISIBLE);
        } else {
            mErrorLayout.setErrorType(EmptyLayout.NODATA);
        }
    }

    @Override
    public void onLoadMoreSuccess(List<Model> data) {
        mAdapter.addAll(data);
    }

    @Override
    public void showMoreMore() {
        mAdapter.setState(BaseRecyclerAdapter.STATE_NO_MORE, true);
    }

    @Override
    public void showNetworkError(int strId) {
        if(strId == 0){
            mAdapter.setState(BaseRecyclerAdapter.STATE_INVALID_NETWORK, true);
        }else{
            if(mErrorLayout != null){
                mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
            }
        }
    }

    @Override
    public void onComplete() {
        mRefreshLayout.onComplete();
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.mPresenter = presenter;
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    public Date getSystemTime() {
        return new Date();
    }

    protected abstract BaseRecyclerAdapter<Model> getAdapter();

    protected abstract void onItemClick(Model model, int position);

    @Override
    public boolean getBackTopVisible() {
        return false;
    }

    @Override
    public boolean getPublishVisible() {
        return false;
    }

    @Override
    public View.OnClickListener getPublishClickListener() {
        return (View.OnClickListener)null;
    }

    public void setErrorImg(int src){
        mErrorLayout.setNoDataImg(src);
    }

    public void setNoDataTip(String tip){
        mErrorLayout.setNoDataContent(tip);
    }

}
